---@class Battle.SkillRangeTipComponent:Battle.SkillComponent
local SkillRangeTipComponent = Battle.CreateClass("SkillRangeTipComponent",Battle.SkillComponent)
Battle.SkillRangeTipComponent = SkillRangeTipComponent

local DEFAULT_WIDTH = 5

function SkillRangeTipComponent:OnInitialized()
    self.loadRequestId = -1
    self.pos = Vector3.New()
    self.forward = Vector3.New()
    ---@type Battle.SkillRangeTipComponent
    local com = self.skill:GetComponent(Battle.SkillReleaseEmitter:GetType())
    self.selectorType = com.rangeSelector.GetType()
    if self.selectorType == Battle.RangeSelectorType.Sector then
        self.resName = "Prefab/Effect/battle/jinengtishi_quan"
    else
        self.resName = "Prefab/Effect/battle/jinengtishi_tiao"
    end
    self.arrowName = "Prefab/Effect/battle/fct_skill_Juli"
end
function SkillRangeTipComponent:OnDestroy()
    self:Hide()
end
function SkillRangeTipComponent:Show(x,y,z)
    if not self.isTouch then
        self.isTouch = true
        if self.loadRequestId == -1 and self.go == nil then
            self.loadRequestId = Battle.PoolMgr.LoadPrefabFromPool(self.resName, self.LoadCallback, self)
            Battle.PoolMgr.LoadPrefabFromPool(self.arrowName,self.LoadArrowCallback,self)
        end
    end
    self.isHide = false
    if self.skill.skillData.rangeScopeParam.type == Battle.SkillScopeType.HandLine then
        local f = self.skill.emitter:GetReleaseForward()
        local p = self.skill.actor.pos
        self:SetForward(f.x,f.y,f.z)
        self:SetPos(p.x,p.y,p.z)
    else
        self:SetPos(x,y,z)
    end
    self.skill.actor.eventSystem:Register(Battle.Event.ActorPosChanged,self.ActorPosChanged,self)
    self.skill.actor.eventSystem:Register(Battle.Event.ActorDead,self.ActorDead,self)
end
function SkillRangeTipComponent:Hide()
    if self.isHide then return end
    self.isHide = true
    self.isTouch = false
    Battle.PoolMgr.CancelLoad(self.loadRequestId)
    Battle.PoolMgr.ReturnPrefabToPool(self.resName,self.go)
    self.go = nil
    self.loadRequestId = -1
    Battle.PoolMgr.ReturnPrefabToPool(self.arrowName,self.arrowGo)
    self.arrowGo = nil
    self.skill.actor.eventSystem:UnRegister(Battle.Event.ActorPosChanged,self.ActorPosChanged,self)
    self.skill.actor.eventSystem:UnRegister(Battle.Event.ActorDead,self.ActorDead,self)
end
function SkillRangeTipComponent:LoadCallback(go)
    ---@type GameObject
    self.go = go
    self.loadRequestId = nil
    local width = self.skill.skillData.rangeScopeParam.width
    local height = self.skill.skillData.rangeScopeParam.height
    Battle.SceneMgr.AddEffectChild(self.go.transform)
    self:SetPos(self.pos.x,self.pos.y,self.pos.z)
    if self.selectorType == Battle.RangeSelectorType.Sector then
        local scale = width/DEFAULT_WIDTH * 2
        self:SetScale(scale,1,scale)
    else
        self:SetScale(width/DEFAULT_WIDTH,1,height/DEFAULT_WIDTH)
    end
end
function SkillRangeTipComponent:SkillStart()

    if self.skill.skillData.rangeScopeParam.width == nil then return end
    if self.skill.skillData.f_TipsRange ~= 1 then return end
    local effect
    if self.skill.actor.camp == Battle.CampType.Blue then
        effect = Battle.EffectShowMgr.PlayEffect("jinengtishi_quan",self.skill.emitter:GetReleasePos(),nil,1)
    else
        effect = Battle.EffectShowMgr.PlayEffect("jinengtishi_quan_02",self.skill.emitter:GetReleasePos(),nil,1)
    end
    local width = self.skill.skillData.rangeScopeParam.width
    local scale = width/DEFAULT_WIDTH * 2
    effect:SetScale(scale)
end
function SkillRangeTipComponent:LoadArrowCallback(go)
    ---@type GameObject
    self.arrowGo = go
    ---@type SkillArrow
    self.arrowScript = self.arrowGo:GetComponent(typeof(SkillArrow))
    if not self.arrowScript then
        logError("缺少skillarrow 脚本")
        return
    end
    Battle.SceneMgr.AddEffectChild(self.arrowGo.transform)

end
function SkillRangeTipComponent:UpdateArrowPos()
    if not self.arrowScript then return end
    local p = self.skill.actor.pos
    self.arrowScript:SetStart(p.x,p.y,p.z)
    self.arrowScript:SetEnd(self.pos.x,self.pos.y,self.pos.z)
    local y = math.min(Battle.BattleUtil.Distance(p,self.pos)/20,1)
    y = math.max(y,0.1)

    self.arrowScript:SetYScale(y)
    self.arrowScript:UpdateLine()
end
function SkillRangeTipComponent:SetPos(x,y,z)
    self.pos:Set(x,0.1,z)
    if self.go then
        Util.SetPosition(self.go,x,0.1,z)
    end
    self:UpdateArrowPos()
end
function SkillRangeTipComponent:SetForward(x,y,z)
    self.forward:Set(x,y,z)
    if self.go then
        self.go.transform.right = -self.forward
    end
end
function SkillRangeTipComponent:SetScale(x,y,z)
    if self.go then
        Util.SetLocalScale(self.go,x,y,z)
    end
end
function SkillRangeTipComponent:ActorPosChanged()
    self:UpdateArrowPos()
end
function SkillRangeTipComponent:ActorDead()
    self:Hide()
end